package com.me.videoservice.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.me.videoservice.dao.DeviceInfoDao;
import com.me.videoservice.model.common.CallbackRetCode;
import com.me.videoservice.model.entry.DeviceInfo;
import com.me.videoservice.model.entry.DeviceInfoWithStream;
import com.me.videoservice.model.entry.DeviceInfoWithStreamVo;
import com.me.videoservice.model.entry.DeviceLocationInfo;
import com.me.videoservice.model.entry.PublishInfo;
import com.me.videoservice.service.DeviceInfoService;
import com.me.videoservice.service.DeviceLocationInfoService;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DeviceInfoServiceImpl extends ServiceImpl<DeviceInfoDao, DeviceInfo> implements
    DeviceInfoService {

  @Autowired
  private DeviceLocationInfoService locationInfoService;

  @Override
  public List<DeviceInfo> queryAll() {
    return this.query().list();
  }

  @Override
  public List<DeviceInfo> queryAllDeviceInfoWithLocation() {
    return this.baseMapper.deviceDetail();
  }

  @Override
  public Integer exist(String stream) {
    return this.baseMapper.exist(stream);
  }

  @Override
  public boolean saveDevice(DeviceInfo deviceInfo) {
    if (deviceInfo.getId() != null) {
      DeviceInfo info = this.getById(deviceInfo.getId());
      if (info != null) {
        locationInfoService.lambdaUpdate()
            .set(DeviceLocationInfo::getDeviceId, deviceInfo.getDeviceId())
            .eq(DeviceLocationInfo::getDeviceId, info.getDeviceId()).update();
      } else {
        deviceInfo.setOfflineFlag(2);
        deviceInfo.setPushFlag(2);
      }
    }
    return this.saveOrUpdate(deviceInfo);
  }

  @Override
  public Integer checkClientAndStreamExist(String clientId, String stream) {
    return this.baseMapper.checkClientAndStreamExist(clientId, stream) == null
        ? CallbackRetCode.NO_DEVICE.getCode() : CallbackRetCode.OK.getCode();
  }

  @Override
  public List<DeviceInfoWithStream> queryAllInfoWithPublish() {
    List<DeviceInfoWithStreamVo> publish = this.baseMapper
        .queryAllInfoWithPublish();
    if (publish != null) {
      return publish.stream().map(it -> {
        DeviceInfoWithStream info = new DeviceInfoWithStream();
        info.setId(it.getId());
        info.setDeviceId(it.getDeviceId());
        info.setOfflineFlag(it.getOfflineFlag());
        info.setStreamCode(it.getStreamCode());
        info.setPushFlag(it.getPushFlag());
        PublishInfo o = new PublishInfo();
        int o_i = 0;
        int i_i = 1;
        PublishInfo i = new PublishInfo();
        if (it.getApps() != null) {
          String[] split = it.getApps().split(",");
          if (split.length > 1) {
            if (!split[0].equals("ordinary")) {
              o_i = 1;
              i_i = 0;
            }
            o.setApp(split[o_i]);
            o.setPushFlag(Integer.valueOf(it.getPushFlags().split(",")[o_i]));
            o.setVhost(it.getVhosts().split(",")[o_i]);
            i.setApp(split[i_i]);
            i.setPushFlag(Integer.valueOf(it.getPushFlags().split(",")[i_i]));
            i.setVhost(it.getVhosts().split(",")[i_i]);
          } else if (split.length > 0) {
            if (split[0].equals("ordinary")) {
              o.setApp(split[0]);
              o.setPushFlag(Integer.valueOf(it.getPushFlags().split(",")[0]));
              o.setVhost(it.getVhosts().split(",")[0]);
            } else {
              i.setApp(split[0]);
              i.setPushFlag(Integer.valueOf(it.getPushFlags().split(",")[0]));
              i.setVhost(it.getVhosts().split(",")[0]);
            }
          }
        }
        info.setOrdinary(o);
        info.setInfrared(i);
        return info;
      }).collect(Collectors.toList());
    }
    return new ArrayList<>();
  }
}
